import release from '@/api/release.js'
import dynamicApi from '@/api/dynamic.js'
import Util from '@/api/util.js'

export default {
    data() {
        return {
            dlGroup: false, // 开团弹窗变量
            dlShare: false, // 动态、邀请帮卖弹窗变量
            dlPoster: false, // 分享海报图片变量
            dlAttendOfficial: false, // 关注公众号变量
            attendOfficialText: '',
            shareObject: {},// 分享海报对象
        }
    },
    // 分享
    methods: {
        // 动态卡片按钮组
        // type: 0--动态分享、2--邀请帮卖、1--一键帮卖
        _dynamicGroupBtn({item, type}) {
            switch (type) {
                case 0:
                case 2:
                    this.shareObject = {
                        ...item,
                        isShareType: type
                    }
                    this.dlShare = true
                    break
                case 1:
                    this._oneHelpSell(item)
                    break
            }
        },
        // 一键帮卖海报绘制
        _oneHelpSell(data) {
            this.shareObject.title = data.title || data.content
            this.shareObject.type = 2
            this.shareObject.dynamicId = data.dynamicId
            const canvasData = {
                userAvatar: this.$store.state.userInfo.communityLogo || this.$store.state.userInfo.avatar,// 团长头像
                userNickname: this.$store.state.userInfo.communityName || this.$store.state.userInfo.nickname,// 名字
                bgImgUrl: data.thumbnail,// 背景图片
                btnText: '立即跟团',
                salePrice: data.minSalePrice,
                type: 2, // 0动态 1开团成功,团购成功 2帮卖
                followGroupNumber: data.totalBuyCount > 0 ? this.playvolumeFn(data.totalBuyCount) + '人跟团中' : '一大波人赶来中'
            }
            // 区别是视频\图文
            if (data.mediaType === 1) {
                canvasData.isGoods = true
            }
            if (data.marketPrice && data.marketPrice > 0) {
                canvasData.supplyPrice = `￥${data.marketPrice}`
            }
            release.oneKeyHelpSell({dynamicIdLv1: data.dynamicId}).then(res => {
                this.shareObject.dynamicId = res.data
                if (res.code==4014) { // 已经帮卖过
                    this.isSoldForHelpSellShow = true
                    this.soldForHelpSellId = res.data
                    let soldForHelpSellData = {
                        isSoldForHelpSellShow: true,
                        soldForHelpSellId: res.data
                    }
                    this.$store.commit('PUBLIC_UPDATE_DATA',soldForHelpSellData) // 首页以外的其他页面用数据仓库的形式在实现
                    data.loadingBtn = false
                } else if (res.code==200) {
                    // 一键帮卖成功之后关注
                    if (data.followStatus === 0) {
                        dynamicApi.follow({ followAccountId: data.accountId, isFollow: 1 }).then(res => {
                            if (res.code === 200) {
                                data.followStatus = 1
                            }
                        })
                    }
                    this.$nextTick(() => {
                        this.$refs.shareChild.shareFrends(canvasData)
                    })
                    setTimeout(() => {
                        data.loadingBtn = false
                    }, 500)
                } else {
                    data.loadingBtn = false
                }
            }).catch(() => {
                data.loadingBtn = false
            })
        },
        // 海报绘制成功--开团
        _shareSuccess(res) {
            if (res.code == 200) {
                if (res.type == 1) {
                    this.shareObject.type = 4
                } else {
                    this.shareObject.type = 2
                }
                if (!this.shareObject.title) {
                    this.shareObject.title = '发布了一个新的视频团购，快来一键帮卖，轻松赚佣金！'
                }
                this.shareObject.url = this.$store.state.openGroupShareData.url
                this.dlGroup = true
            }
        },
        // 动态分享、邀请帮卖海报绘制成功
        _posterFn(data) {
            this.shareObject = data
            this.dlShare = false
            this.dlPoster = true
        },
        playvolumeFn (mun) {
            return Util.playvolume(mun)
        },
    }
}
